/**
 * @author YinHang
 */
;(function () {
	var Sprite = GameFramework.inherit("ui.g2d.Displayable")(function (image, sWidth, sHeight) {
		if(!image || !image.loaded)	
			return null;
		this.image = image;
		this.xFrameCount = parseInt(image.getWidth() / sWidth);
		this.yFrameCount = parseInt(image.getHeight() / sHeight);
		this.frameCount = this.xFrameCount * this.yFrameCount;
		this.sHeight = sHeight;
		this.sWidth = sWidth;
		this.curFrame = 1;
		this.setFrame(this.curFrame);
		this.targetX = null;
		this.targetY = null;
		this.targetXSpeed = null;
		this.targetYSpeed = null;
		Sprite.base.call(this, 0, 0, this.sWidth, this.sHeight, true);
	});
	
	Sprite.prototype.getFrameCount = function () {
		return this.frameCount;
	};
	
	Sprite.prototype.loopFrame = function () {
		if(this.nextFrame() == false)
			this.setFrame(1);
	};
	
	Sprite.prototype.nextFrame = function () {
		if(this.curFrame < this.frameCount)
		{
			this.setFrame(++ this.curFrame);
			return true;
		}
		else
		{
			return false;
		}
	};
	
	Sprite.prototype.prevFrame = function () {
		if(this.curFrame > 1)
			this.setFrame(-- this.curFrame);
	};
	
	Sprite.prototype.setFrame = function (frame) {
		if(frame > 0 && frame <= this.frameCount)
		{
			var trueFrame = frame - 1;
			this.frameX = (trueFrame % this.xFrameCount) * this.sWidth;
			this.frameY = parseInt(trueFrame / this.xFrameCount) * this.sHeight;
			this.curFrame = frame;
		}
	};
	
	Sprite.prototype.move = function (dx, dy) {
		this.setX(this.getX() + dx);
		this.setY(this.getY() + dy);
	};
	
	Sprite.prototype.setTarget = function (targetX, targetY, speed) {
		this.targetX = targetX;
		this.targetY = targetY;
		var a = targetX - this.x;
		var b = targetY - this.y;
		var length = Math.sqrt(a * a + b * b);
		var time = length / speed;
		this.targetXSpeed = a / time;
		this.targetYSpeed = b / time;
	};
	
	Sprite.prototype.moveToTarget = function () {
		if(Math.abs(this.targetX - this.x) >= Math.abs(this.targetXSpeed))
		{
			this.move(this.targetXSpeed, 0);
		}
		else
		{
			this.setX(this.targetX);
		}
		if(Math.abs(this.targetY - this.y) >= Math.abs(this.targetYSpeed))
		{
			this.move(0, this.targetYSpeed);
		}
		else
		{
			this.setY(this.targetY);
		}
	};
	
	Sprite.prototype.moveTo = function (targetX, targetY, speed) {
		if(this.x != targetX || this.y != targetY)
		{
			var a = targetX - this.x;
			var b = targetY - this.y;
			var length = Math.sqrt(a * a + b * b);
			var time = length / speed;
			var xSpeed = a / time;
			var ySpeed = b / time;
			if(Math.abs(a) >= Math.abs(xSpeed))
			{
				this.move(xSpeed, 0);
			}
			else
			{
				this.setX(targetX);
			}
			if(Math.abs(b) >= Math.abs(ySpeed))
			{
				this.move(0, ySpeed);
			}
			else
			{
				this.setY(targetY);
			}
		}
	};
	
	Sprite.prototype.getFrame = function () {
		return this.curFrame;
	};
	
	Sprite.prototype.draw = function (graphics) {
		graphics.getContext().drawImage(this.image.getSource(), this.frameX, this.frameY, this.sWidth, this.sHeight, this.x, this.y, this.width, this.height);
	};
	
	GameFramework.register(Sprite);
})();
